package com.fare.taluo.core.domain.queue.message;

import java.util.Date;

import javax.mail.MessagingException;

import com.fare.taluo.message.MessageConfiguration;
import com.foreveross.threadpool.queue.WorkQueue;
/**
 * 消息队列创建工厂
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-24-上午10:06:00
 * @version v1.0
 */
public class MessageCreatorFactory {
	/**
	 * 创建邮件任务
	 * <p>队列名取默认值：邮件发送：+subject<br/>
	 * 发送人为message.properties中配置的地址
	 * @param subject 邮件主题
	 * @param content 邮件内容，支持html格式
	 * @param toAddress 发送目标邮箱，多个以;号分隔
	 * @param planStartTime 任务执行的开始时间
	 * @return 实体Entity对象
	 * @throws MessagingException 实例化失败将抛出该异常
	 */
	public static QueueEmailMessageEntity createEmailQueue(String subject, String content,
			String toAddress, Date planStartTime) throws MessagingException{
		QueueEmailMessageEntity entity = new QueueEmailMessageEntity();
		entity.setBody(content);
		entity.setSubject(subject);
		entity.setCreateTime(new Date());
		entity.setFromAddress(MessageConfiguration.getInstance().getEmailProperties().getSendAddress());
		entity.setName("邮件发送："+subject);
		entity.setPlanStartTime(planStartTime);
		entity.setStatus(WorkQueue.STATUS_WAITING);
		entity.setToAddress(toAddress);
		return entity;
	}
	/**
	 * 创建邮件任务
	 * <p>队列名取默认值：邮件发送：+subject<br/>
	 * 发送地址，发送目标地址为message.properties中配置的地址<br/>
	 * 任务执行时间为系统当前时间
	 * @param subject 邮件主题
	 * @param content 邮件内容，支持html格式
	 * @return 实体Entity对象
	 * @throws MessagingException 实例化失败将抛出该异常
	 */
	public static QueueEmailMessageEntity createEmailQueue(String subject, String content) throws MessagingException{
		QueueEmailMessageEntity entity = new QueueEmailMessageEntity();
		entity.setBody(content);
		entity.setSubject(subject);
		entity.setCreateTime(new Date());
		entity.setFromAddress(MessageConfiguration.getInstance().getEmailProperties().getSendAddress());
		entity.setName("邮件发送："+subject);
		entity.setPlanStartTime(new Date());
		entity.setStatus(WorkQueue.STATUS_WAITING);
		entity.setToAddress(MessageConfiguration.getInstance().getEmailProperties().getDefaultToAddress());
		return entity;
	}
	/**
	 * 创建短信消息队列
	 * <p>默认的队列名称为：短信发送：+toAddress
	 * @param toAddress 目标手机号
	 * @param content 发送内容
	 * @param planStartTime 任务执行开始时间
	 * @return 实体Entity对象
	 */
	public static QueueSMSMessageEntity createSMSQueue(String toAddress, String content, Date planStartTime){
		QueueSMSMessageEntity entity = new QueueSMSMessageEntity();
		entity.setContent(content);
		entity.setCreateTime(new Date());
		entity.setName("短信发送:"+toAddress);
		entity.setPlanStartTime(planStartTime);
		entity.setStatus(WorkQueue.STATUS_WAITING);
		entity.setToPhone(toAddress);
		return entity;
	}
	
	/**
	 * 创建短信消息队列
	 * <p>默认的队列名称为：短信发送：+toAddress<br/>
	 * 发送目标手机号为message.properties中配置接收人地址<br/>
	 * 任务开始执行时间为当前时间
	 * @param content 发送内容
	 * @return 实体Entity对象
	 */
	public static QueueSMSMessageEntity createSMSQueue(String content){
		QueueSMSMessageEntity entity = new QueueSMSMessageEntity();
		String toAddress = MessageConfiguration.getInstance().getSmsProperties().getDefaultToPhone();
		entity.setContent(content);
		entity.setCreateTime(new Date());
		entity.setName("短信发送:"+toAddress);
		entity.setPlanStartTime(new Date());
		entity.setStatus(WorkQueue.STATUS_WAITING);
		entity.setToPhone(toAddress);
		return entity;
	}
}
